package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.server.interceptor.UserThreadLocal;
import com.tanhua.server.service.MovementsService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("comments")
public class CommentsController {

    @Autowired
    private MovementsService movementsService;


    /**
     * 查询评论列表
     */
    @GetMapping
    public ResponseEntity<PageResult> queryCommentsList(@RequestParam("movementId") String publishId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.movementsService.queryCommentsList(publishId, page, pageSize);
    }



    /**
     * 发表评论
     */
    @PostMapping
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param) {
        String publishId = param.get("movementId");
        String content = param.get("comment");
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setContent(content);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.saveComment(comment);
    }

    /**
     * 点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);
        comment.setPubType(3);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.saveComment(comment);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);
        comment.setPubType(3);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.removeComment(comment);
    }


}
